
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   specific_test.py
@Time    :   2023/4/2 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   YangLiang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
import binascii
import datetime
import time
from pytest_assume.plugin import assume
import sys
import os
import logging
# logging.getLogger().setLevel(logging.INFO)
import allure
import pytest
from cryptography.hazmat.primitives.asymmetric import rsa

# curPath = os.path.abspath(os.path.dirname(__file__))
# rootPath = os.path.abspath(os.path.dirname(curPath) + os.path.sep + ".")
# sys.path.append(rootPath)
# sys.path.append(os.path.join(curPath,'commDriver'))
# sys.path.append(os.path.join(curPath,'data'))
# logging.info(sys.path)

from commDriver.TS_CAN import TSCAN, TSCAN_UDS,HIL_Obj
from commDriver.TS_CAN import TSMasterAPI
import Set_up
from commDriver.modbus_plc import *

sys.dont_write_bytecode = True
import Set_up

Enable_check_PCAN_CheckRoll = False
PCAN_rolling_counter={}

Enable_check_VCAN_CheckRoll = False
VCAN_rolling_counter={}

Enable_warning_sim = False
warning_ID_ref = None

check_ID_VList = [0x44C,0x449]

check_ID_List_RL = [0x280,0x281,0x282,0x283,0x284,0x285,0x286,0x287,0x288,0x289,0x28A,0x28B,0x28C,0x28D,0x28E,0x28F,0x32C]#,0x32D]
check_ID_List_RR = [0x2C0,0x2C1,0x2C2,0x2C3,0x2C4,0x2C5,0x2C6,0x2C7,0x2C8,0x2C9,0x2CA,0x2CB,0x2CC,0x2CD,0x2CE,0x2CF,0x32A]
check_ID_List = check_ID_List_RL + check_ID_List_RR
# check_ID_List = [0x280]


def Enable_check_PCAN_CheckRoll_fun(enable):
    global Enable_check_PCAN_CheckRoll
    global PCAN_rolling_counter
    if enable:
        Enable_check_PCAN_CheckRoll =True
    else:
        Enable_check_PCAN_CheckRoll =False
        PCAN_rolling_counter={}


def PCAN_Check_F(OBJ, ACAN):
    global Enable_check_PCAN_CheckRoll
    global PCAN_rolling_counter

    ID_ststus = {'counter':0,'lossframe':False}
    current_add_OK = True

    current_ID = ACAN.contents.FIdentifier

    # if Set_up.act_radar_type == 'LFCR' or Set_up.act_radar_type == 'LFCR':
    #     check_ID_List = check_ID_List_F
    #     pass
    # elif Set_up.act_radar_type == 'LFCR' or Set_up.act_radar_type == 'LFCR':
    #     check_ID_List = check_ID_List_R

    # if (current_ID in check_ID_List and ACAN.contents.FIdxChn == 1): # Done
    if (current_ID in check_ID_List): # Done
        if current_ID in [0x32C,0x32A]:
            # Checksum
            act_checksum = ACAN.contents.FData[7]
            exc_checksum = sum(ACAN.contents.FData[0:7]) & 0xFF
            if act_checksum != exc_checksum:
                logging.info(f'{hex(current_ID)} act_checksum: {hex(act_checksum)} exc_checksum:{hex(exc_checksum)}')
            with assume: assert act_checksum == exc_checksum

            current_counter = ACAN.contents.FData[6] & 0xF
            if Enable_check_PCAN_CheckRoll == True:
                if  PCAN_rolling_counter.__contains__(hex(current_ID)):
                    if PCAN_rolling_counter[hex(current_ID)]['counter'] == 15:
                        current_add_OK =  (current_counter==0)
                        # logging.info(f'counter15==0:{current_counter}')
                    else:
                        current_add_OK =  ( PCAN_rolling_counter[hex(current_ID)]['counter'] + 1 == current_counter )
                        # logging.info(f'counter+1:{current_counter}')

                    ID_ststus = {'counter':current_counter,'lossframe':PCAN_rolling_counter[hex(current_ID)]['lossframe'] & current_add_OK}

                    PCAN_rolling_counter[hex(current_ID)] = ID_ststus
                else:
                    ID_ststus = {'counter':current_counter,'lossframe':current_add_OK}
                    PCAN_rolling_counter[hex(current_ID)] = ID_ststus
                logging.info(PCAN_rolling_counter)
        
        elif current_ID in [0x32D]:
            # Checksum
            act_checksum1 = ACAN.contents.FData[7]
            exc_checksum1 = (sum(ACAN.contents.FData[0:7])) & 0xFF
            if act_checksum1 != exc_checksum1:
                logging.info(f'{hex(current_ID)} act_checksum: {hex(act_checksum1)} exc_checksum:{hex(exc_checksum1)}')
            with assume: assert act_checksum1 == exc_checksum1

            act_checksum2 = ACAN.contents.FData[15]
            exc_checksum2 = (sum(ACAN.contents.FData[8:15])) & 0xFF
            if act_checksum2 != exc_checksum2:
                logging.info(f'{hex(current_ID)} act_checksum: {hex(act_checksum2)} exc_checksum:{hex(exc_checksum2)}')
            with assume: assert act_checksum2 == exc_checksum2

            act_checksum3 = ACAN.contents.FData[23]
            exc_checksum3 = (sum(ACAN.contents.FData[16:23])) & 0xFF
            if act_checksum3 != exc_checksum3:
                logging.info(f'{hex(current_ID)} act_checksum: {hex(act_checksum3)} exc_checksum:{hex(exc_checksum3)}')
            with assume: assert act_checksum3 == exc_checksum3

            act_checksum4 = ACAN.contents.FData[31]
            exc_checksum4 = (sum(ACAN.contents.FData[24:31])) & 0xFF
            if act_checksum4 != exc_checksum4:
                logging.info(f'{hex(current_ID)} act_checksum: {hex(act_checksum4)} exc_checksum:{hex(exc_checksum4)}')
            with assume: assert act_checksum4 == exc_checksum4

            current_counter = ACAN.contents.FData[30]&0xF
            if Enable_check_PCAN_CheckRoll == True:
                if  PCAN_rolling_counter.__contains__(hex(current_ID)):
                    if PCAN_rolling_counter[hex(current_ID)]['counter'] == 15:
                        current_add_OK =  (current_counter==0)
                        # logging.info(f'counter15==0:{current_counter}')
                    else:
                        current_add_OK =  ( PCAN_rolling_counter[hex(current_ID)]['counter'] + 1 == current_counter )
                        # logging.info(f'counter+1:{current_counter}')

                    ID_ststus = {'counter':current_counter,'lossframe':PCAN_rolling_counter[hex(current_ID)]['lossframe'] & current_add_OK}

                    PCAN_rolling_counter[hex(current_ID)] = ID_ststus
                else:
                    ID_ststus = {'counter':current_counter,'lossframe':current_add_OK}
                    PCAN_rolling_counter[hex(current_ID)] = ID_ststus
                logging.info(PCAN_rolling_counter)

        else:
            # Checksum
            act_checksum = ACAN.contents.FData[0]
            exc_checksum = (sum(ACAN.contents.FData[1:24]) ^ 0xFF) & 0xFF
            if act_checksum != exc_checksum:
                logging.info(f'{hex(current_ID)} act_checksum: {hex(act_checksum)}')
                logging.info(f'{hex(current_ID)} exc_checksum:{hex(exc_checksum)}')
            with assume: assert act_checksum == exc_checksum

            # rolling counter
            current_counter = ACAN.contents.FData[23] & 0x0F
            if Enable_check_PCAN_CheckRoll == True:
                if  PCAN_rolling_counter.__contains__(hex(current_ID)):
                    if PCAN_rolling_counter[hex(current_ID)]['counter'] == 15:
                        current_add_OK =  (current_counter==0)
                        # logging.info(f'counter15==0:{current_counter}')
                    else:
                        current_add_OK =  ( PCAN_rolling_counter[hex(current_ID)]['counter'] + 1 == current_counter )
                        # logging.info(f'counter+1:{current_counter}')

                    ID_ststus = {'counter':current_counter,'lossframe':PCAN_rolling_counter[hex(current_ID)]['lossframe'] & current_add_OK}

                    PCAN_rolling_counter[hex(current_ID)] = ID_ststus
                else:
                    ID_ststus = {'counter':current_counter,'lossframe':current_add_OK}
                    PCAN_rolling_counter[hex(current_ID)] = ID_ststus
                logging.info(PCAN_rolling_counter)
                # logging.info(str(ID_ststus)+'---------------'+hex(current_ID))
    


PCAN_Check_F_ref = TSMasterAPI.OnTx_RxFUNC_CANFD(PCAN_Check_F)

def Enable_check_VCAN_CheckRoll_fun(enable):
    global Enable_check_VCAN_CheckRoll
    global VCAN_rolling_counter
    if enable:
        Enable_check_VCAN_CheckRoll =True
    else:
        Enable_check_VCAN_CheckRoll =False
        VCAN_rolling_counter={}


def VCAN_Check_F(OBJ, ACAN):
    global Enable_check_VCAN_CheckRoll
    global VCAN_rolling_counter

    ID_ststus = {'counter':0,'lossframe':False}
    current_add_OK = True

    current_ID = ACAN.contents.FIdentifier

    if (current_ID in check_ID_VList): # Done
        # Checksum
        act_checksum = ACAN.contents.FData[0]
        exc_checksum = sum(ACAN.contents.FData[1:8]) & 0xFF
        if act_checksum != exc_checksum:
            logging.info(f'{hex(current_ID)} act_checksum: {hex(act_checksum)}')
            logging.info(f'{hex(current_ID)} exc_checksum:{hex(exc_checksum)}')
        with assume: assert act_checksum == exc_checksum 

        current_counter = ACAN.contents.FData[1] & 0xF
        if Enable_check_VCAN_CheckRoll == True:
            if  VCAN_rolling_counter.__contains__(hex(current_ID)):
                if VCAN_rolling_counter[hex(current_ID)]['counter'] == 15:
                    current_add_OK =  (current_counter==0)
                    # logging.info(f'counter15==0:{current_counter}')
                else:
                    current_add_OK =  ( VCAN_rolling_counter[hex(current_ID)]['counter'] + 1 == current_counter )
                    # logging.info(f'counter+1:{current_counter}')

                ID_ststus = {'counter':current_counter,'lossframe':VCAN_rolling_counter[hex(current_ID)]['lossframe'] & current_add_OK}

                VCAN_rolling_counter[hex(current_ID)] = ID_ststus
            else:
                ID_ststus = {'counter':current_counter,'lossframe':current_add_OK}
                VCAN_rolling_counter[hex(current_ID)] = ID_ststus
            logging.info(VCAN_rolling_counter)
            # logging.info(str(ID_ststus)+'---------------'+hex(current_ID))
VCAN_Check_F_ref = TSMasterAPI.OnTx_RxFUNC_CANFD(VCAN_Check_F)

def left_rada_signal(Driver,a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0):
    global Left_Radar_FCTB_Braking_Left_S
    global Left_Radar_FCTA_Warning_Left_S
    global Left_Radar_JA_Warning_Left_S
    global Left_Radar_BSD_Warning_Left_S
    global Left_Radar_LCA_Warning_Left_S
    global Left_Rada_DOW_Warning_Left_S
    global Left_Radar_RCTA_Warning_Left_S
    global Left_Radar_RCTB_Braking_Left_S

    Left_Radar_FCTB_Braking_Left_S = a
    Left_Radar_FCTA_Warning_Left_S = b
    Left_Radar_JA_Warning_Left_S = c
    Left_Radar_BSD_Warning_Left_S = d
    Left_Radar_LCA_Warning_Left_S = e
    Left_Rada_DOW_Warning_Left_S = f
    Left_Radar_RCTA_Warning_Left_S = g
    Left_Radar_RCTB_Braking_Left_S = h
    cycle = Driver['CAN2'].send_CANFrame_Cycle(0x32C,[(d << 2)+ (f<<4) + (g << 6),(h << 2)+(a << 3),0x00,0x00,(b << 4)+c,e << 4,0x00,0x00],50,cycle_CAN_Type='CANFD')
    return cycle

def VCAN_Check_signal(OBJ, ACAN):
    global Left_Radar_FCTB_Braking_Left_S
    global Left_Radar_FCTA_Warning_Left_S
    global Left_Radar_JA_Warning_Left_S
    global Left_Radar_BSD_Warning_Left_S
    global Left_Radar_LCA_Warning_Left_S
    global Left_Rada_DOW_Warning_Left_S
    global Left_Radar_RCTA_Warning_Left_S
    global Left_Radar_RCTB_Braking_Left_S

    # ID_ststus = {'counter':0,'lossframe':False}
    # current_add_OK = True

    current_ID = ACAN.contents.FIdentifier

    if current_ID == 0x384: # Done
        # 右前雷达FCTB_JA制动
        CMRR_FR_W_RHFCTB_JA = ACAN.contents.FData[1]&0xC0  # 0,1
        # 左前雷达FCTA报警
        CMRR_FR_W_LHFCTA = ACAN.contents.FData[3]&0xC  # 0,1,2,0
        # 左前雷达JA报警
        CMRR_FR_W_LHJA = ACAN.contents.FData[6]&0x3  # 0,1
        if Left_Radar_FCTB_Braking_Left_S == 0:
            with assume: assert CMRR_FR_W_RHFCTB_JA == 0
        else:
            with assume: assert CMRR_FR_W_RHFCTB_JA == 1
        
        if Left_Radar_FCTA_Warning_Left_S == 0 or Left_Radar_FCTA_Warning_Left_S == 3:
            with assume: assert CMRR_FR_W_LHFCTA == 0
        elif Left_Radar_FCTA_Warning_Left_S == 1:
            with assume: assert CMRR_FR_W_LHFCTA == 1
        else:
            with assume: assert CMRR_FR_W_LHFCTA == 2

        if Left_Radar_JA_Warning_Left_S == 0:
            with assume: assert CMRR_FR_W_LHJA == 0
        else:
            with assume: assert CMRR_FR_W_LHJA == 1

    if current_ID == 0x385:
        # 左后雷达BSD报警
        CMRR_RR1_W_LHBSD = ACAN.contents.FData[3]&0x3  # 0,1,2,0
        # 左后雷达LCA报警
        CMRR_RR1_W_LHLCA = ACAN.contents.FData[3]&0xC  # 0,1,2,0
        # 左后雷达DOW报警
        CMRR_RR1_W_LHDOW = ACAN.contents.FData[3]&0x30  # 0,1,2,0
        # 左后雷达RCTA报警
        CMRR_RR1_W_LHRCTA = ACAN.contents.FData[3]&0xC0  # 0,1,2,0
        #右雷达RCTB制动
        CMRR_RR1_W_RHRCTB = ACAN.contents.FData[4]&0x3  # 0,1

        if Left_Radar_BSD_Warning_Left_S == 0 or Left_Radar_BSD_Warning_Left_S == 3:
            with assume: assert CMRR_RR1_W_LHBSD == 0
        elif Left_Radar_BSD_Warning_Left_S == 1:
            with assume: assert CMRR_RR1_W_LHBSD == 1
        else:
            with assume: assert CMRR_RR1_W_LHBSD == 2

        if Left_Radar_LCA_Warning_Left_S == 0 or Left_Radar_LCA_Warning_Left_S == 3:
            with assume: assert CMRR_RR1_W_LHLCA == 0
        elif Left_Radar_BSD_Warning_Left_S == 1:
            with assume: assert CMRR_RR1_W_LHLCA == 1
        else:
            with assume: assert CMRR_RR1_W_LHLCA == 2

        if Left_Rada_DOW_Warning_Left_S == 0 or Left_Rada_DOW_Warning_Left_S == 3:
            with assume: assert CMRR_RR1_W_LHDOW == 0
        elif Left_Radar_BSD_Warning_Left_S == 1:
            with assume: assert CMRR_RR1_W_LHDOW == 1
        else:
            with assume: assert CMRR_RR1_W_LHDOW == 2

        if Left_Radar_RCTA_Warning_Left_S == 0 or Left_Radar_RCTA_Warning_Left_S == 3:
            with assume: assert CMRR_RR1_W_LHDOW == 0
        elif Left_Radar_BSD_Warning_Left_S == 1:
            with assume: assert CMRR_RR1_W_LHDOW == 1
        else:
            with assume: assert CMRR_RR1_W_LHDOW == 2

        if Left_Radar_RCTB_Braking_Left_S == 0:
            with assume: assert CMRR_RR1_W_RHRCTB == 0
        else:
            with assume: assert CMRR_RR1_W_RHRCTB == 1
        
        

        
VCAN_Check_F_ref_signal = TSMasterAPI.OnTx_RxFUNC_CANFD(VCAN_Check_signal)


# @pytest.mark.repeat(2)
@allure.title('Test specific scenario _check loss frame at PCAN')
@allure.description('Test specific scenario _check loss frame at PCAN')
@allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
def test_Pcan_loss_frame_check_F(action,Driver,power_reset):
    #0x243,0x242与Vcan上ID冲突，虽然设置的是读的Pcan的但是依然能读到VCAN上的
    """
    Pcan CRC and checksum
    """

    global PCAN_rolling_counter
    iStep = 1
    # 设置电压为0V
    Driver['power'].set_vltage(0)
    # 停发唤醒
    Driver['CAN'].wakeup_ctrl(False)
    logging.info('delay 5s')
    time.sleep(5)
    Driver['power'].Read_Measure()

    # 设置电压为12v 
    Driver['power'].set_vltage(12)
    logging.info('delay 5s')
    time.sleep(5)
    Driver['power'].Read_Measure()
    # 使能check lossframe
    # # need todo
    # Driver['CAN3'].register_event_canfd_T_R(PCAN_Check_F_ref)
    # Enable_check_PCAN_CheckRoll_fun(True)

    # 正常唤醒
    Driver['CAN'].wakeup_ctrl(True)
    time.sleep(2)

    Driver['CAN2'].register_event_canfd_T_R(PCAN_Check_F_ref)
    Enable_check_PCAN_CheckRoll_fun(True)

    start_time = time.time()

    while (time.time()-start_time ) < 3:
        # check the pass plag
        # need todo
        # logging.warning(PCAN_rolling_counter)
        for eacc_ID in PCAN_rolling_counter:
            if PCAN_rolling_counter[eacc_ID]['lossframe'] == True:
                pass
            else:
                logging.warning(eacc_ID +''+ f"{PCAN_rolling_counter[eacc_ID]}" +'-------------had lost frame')
                with assume: assert False     
        time.sleep(1)
        pass
    # 关闭 check lossframe
    # Enable_check_PCAN_CheckRoll_fun(False)
    Driver['CAN2'].unregister_event_canfd_T_R(PCAN_Check_F_ref)
    time.sleep(2)

# APP报文没有CRC和counter
# @pytest.mark.repeat(1)
# @allure.title('Test specific scenario _check loss frame at VCAN')
# @allure.description('Test specific scenario _check loss frame at VCAN')
# @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
# def test_Vcan_loss_APP_frame_check_F(action,Driver,power_reset):
#     """
#     Vcan NM frame CRC and checksum
#     """
#     global VCAN_rolling_counter
#     iStep = 1
#     # 设置电压为0V
#     Driver['power'].set_vltage(0)
#     # 停发唤醒
#     Driver['CAN'].wakeup_ctrl(False)
#     logging.info('delay 5s')
#     time.sleep(5)
#     Driver['power'].Read_Measure()

#     # 设置电压为12v 
#     Driver['power'].set_vltage(12)
#     logging.info('delay 5s')
#     time.sleep(5)
#     Driver['power'].Read_Measure()
#     # 使能check lossframe
#     # # need todo
#     # Driver['CAN2'].register_event_canfd_T_R(VCAN_Check_F_ref)
#     # Enable_check_VCAN_CheckRoll_fun(True)

#     # 正常唤醒
#     Driver['CAN'].wakeup_ctrl(True)
#     time.sleep(1)

#     Driver['CAN'].register_event_canfd_T_R(VCAN_Check_F_ref)
#     Enable_check_VCAN_CheckRoll_fun(True)

#     start_time = time.time()

#     while (time.time()-start_time ) < 5:
#         # check the pass plag
#         # need todo
#         # logging.warning(VCAN_rolling_counter)
#         for eacc_ID in VCAN_rolling_counter:
#             if VCAN_rolling_counter[eacc_ID]['lossframe'] == True:
#                 pass
#             else:
#                 logging.warning(eacc_ID +''+ f"{VCAN_rolling_counter[eacc_ID]}" +'-------------had lost frame')
#                 with assume: assert False     
#         time.sleep(1)
#         pass
#     # 关闭 check lossframe
#     # Enable_check_VCAN_CheckRoll_fun(False)
#     Driver['CAN'].unregister_event_canfd_T_R(VCAN_Check_F_ref)



@pytest.mark.repeat(1)
@allure.title('test_leftradar_transmitting_signal_to_rightradar_Front')
@allure.description('test_leftradar_transmitting_signal_to_rightradar_Front')
@allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
def test_leftradar_transmitting_signal_to_rightradar_Front(action,Driver,power_reset):
    """
    需求:右后角雷达和右前角雷达接收0x32C报文,将左侧雷达相关的信号转发至VCAN
    无法保证时效性

    验证正确性,有效性:方法1:左右都接,验证两报文对应信号是否一致;方法二:左雷达不接,模拟Pcan发0x32C
    """
    iStep = 1
    # 设置电压为0V
    Driver['power'].set_vltage(0)
    # 停发唤醒
    Driver['CAN'].wakeup_ctrl(False)
    logging.info('delay 5s')
    time.sleep(5)
    Driver['power'].Read_Measure()

    # 设置电压为12v 
    Driver['power'].set_vltage(12)
    logging.info('delay 5s')
    time.sleep(5)
    Driver['power'].Read_Measure()

    # 正常唤醒
    Driver['CAN'].wakeup_ctrl(True)
    time.sleep(2)

    # a,b,c 对应左前
    cycle = left_rada_signal(Driver,a=1,b=2,c=0,d=0,e=0,f=0,g=0,h=0)
    time.sleep(1)

    Driver['CAN'].register_event_canfd_T_R(VCAN_Check_F_ref_signal)

    start_time = time.time()
    while (time.time()-start_time ) > 1:
        break
    Driver['CAN'].unregister_event_canfd_T_R(VCAN_Check_F_ref_signal)
    Driver['CAN2'].stop_FrANFrame_Cycle(cycle)
    
    time.sleep(1)
    cycle = left_rada_signal(Driver,a=0,b=1,c=0,d=0,e=0,f=0,g=0,h=0)
    time.sleep(1)

    Driver['CAN'].register_event_canfd_T_R(VCAN_Check_F_ref_signal)

    start_time = time.time()
    while (time.time()-start_time ) > 1:
        break
    Driver['CAN'].unregister_event_canfd_T_R(VCAN_Check_F_ref_signal)
    Driver['CAN2'].stop_FrANFrame_Cycle(cycle)


    time.sleep(1)
    cycle = left_rada_signal(Driver,a=0,b=2,c=1,d=0,e=0,f=0,g=0,h=0)
    time.sleep(1)

    Driver['CAN'].register_event_canfd_T_R(VCAN_Check_F_ref_signal)

    start_time = time.time()
    while (time.time()-start_time ) > 1:
        break
    Driver['CAN'].unregister_event_canfd_T_R(VCAN_Check_F_ref_signal)
    Driver['CAN2'].stop_FrANFrame_Cycle(cycle)


    time.sleep(1)
    cycle = left_rada_signal(Driver,a=1,b=3,c=0,d=0,e=0,f=0,g=0,h=0)
    time.sleep(1)

    Driver['CAN'].register_event_canfd_T_R(VCAN_Check_F_ref_signal)

    start_time = time.time()
    while (time.time()-start_time ) > 1:
        break
    Driver['CAN'].unregister_event_canfd_T_R(VCAN_Check_F_ref_signal)
    Driver['CAN2'].stop_FrANFrame_Cycle(cycle)


@pytest.mark.repeat(1)
@allure.title('test_leftradar_transmitting_signal_to_rightradar_Rear')
@allure.description('test_leftradar_transmitting_signal_to_rightradar_Rear')
@allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
def test_leftradar_transmitting_signal_to_rightradar_Rear(action,Driver,power_reset):
    """
    需求:右后角雷达和右前角雷达接收0x32C报文,将左侧雷达相关的信号转发至VCAN
    无法保证时效性

    验证正确性,有效性:方法1:左右都接,验证两报文对应信号是否一致;方法二:左雷达不接,模拟Pcan发0x32C
    """
    iStep = 1
    # 设置电压为0V
    Driver['power'].set_vltage(0)
    # 停发唤醒
    Driver['CAN'].wakeup_ctrl(False)
    logging.info('delay 5s')
    time.sleep(5)
    Driver['power'].Read_Measure()

    # 设置电压为12v 
    Driver['power'].set_vltage(12)
    logging.info('delay 5s')
    time.sleep(5)
    Driver['power'].Read_Measure()

    # 正常唤醒
    Driver['CAN'].wakeup_ctrl(True)
    time.sleep(2)

    # a,b,c 对应左前
    cycle = left_rada_signal(Driver,a=0,b=0,c=0,d=1,e=2,f=3,g=0,h=0)
    time.sleep(1)

    Driver['CAN'].register_event_canfd_T_R(VCAN_Check_F_ref_signal)

    start_time = time.time()
    while (time.time()-start_time ) > 1:
        break
    Driver['CAN'].unregister_event_canfd_T_R(VCAN_Check_F_ref_signal)
    Driver['CAN2'].stop_FrANFrame_Cycle(cycle)
    
    time.sleep(1)
    cycle = left_rada_signal(Driver,a=0,b=0,c=0,d=2,e=3,f=1,g=3,h=0)
    time.sleep(1)

    Driver['CAN'].register_event_canfd_T_R(VCAN_Check_F_ref_signal)

    start_time = time.time()
    while (time.time()-start_time ) > 1:
        break
    Driver['CAN'].unregister_event_canfd_T_R(VCAN_Check_F_ref_signal)
    Driver['CAN2'].stop_FrANFrame_Cycle(cycle)


    time.sleep(1)
    cycle = left_rada_signal(Driver,a=0,b=0,c=0,d=3,e=1,f=2,g=1,h=0)
    time.sleep(1)

    Driver['CAN'].register_event_canfd_T_R(VCAN_Check_F_ref_signal)

    start_time = time.time()
    while (time.time()-start_time ) > 1:
        break
    Driver['CAN'].unregister_event_canfd_T_R(VCAN_Check_F_ref_signal)
    Driver['CAN2'].stop_FrANFrame_Cycle(cycle)


    time.sleep(1)
    cycle = left_rada_signal(Driver,a=0,b=0,c=0,d=0,e=0,f=0,g=2,h=1)
    time.sleep(1)

    Driver['CAN'].register_event_canfd_T_R(VCAN_Check_F_ref_signal)

    start_time = time.time()
    while (time.time()-start_time ) > 1:
        break
    Driver['CAN'].unregister_event_canfd_T_R(VCAN_Check_F_ref_signal)
    Driver['CAN2'].stop_FrANFrame_Cycle(cycle)


def test_no_busoff_DTC_when_idle(action,Driver,power_reset):
    start_time = datetime.datetime.now()
    logging.info(start_time)
    flag = True
    uds_rep,size = Driver['UDSLRCR'].req_and_res_can([0x14,0xFF,0xFF,0xFF],4)
    with assume: assert uds_rep[0] == [0x54]
    uds_rep,size = Driver['UDSRRCR'].req_and_res_can([0x14,0xFF,0xFF,0xFF],4)
    with assume: assert uds_rep[0] == [0x54]
    while flag:
        time.sleep(10)
        uds_rep,size = Driver['UDSLRCR'].req_and_res_can([0x19,0x02,0x0b],3)
        if 0x88 in uds_rep:
            flag = False
            with assume: assert 1==0
            break
        uds_rep,size = Driver['UDSRRCR'].req_and_res_can([0x19,0x02,0x0b],3)
        if 0x88 in uds_rep:
            flag = False
            with assume: assert 1==0
            break
    end_time = datetime.datetime.now()
    logging.info(end_time)

def test_System_Blockage_DTC(action,Driver,power_reset):
    # 车速超过15kph,行驶5min以上是否报故障
    Driver['CAN'].set_signal_IGN(2)
    Driver['CAN'].set_signal_Speed(16)
    # time.sleep(400)
    time.sleep(900)
    a,b,c = Driver['UDSRRCR'].search_DTC([0x19,0x02,0x0B],[0x98,0x8C,0x00,0x09])
    with assume: assert a == True
    a,b,c = Driver['UDSLRCR'].search_DTC([0x19,0x02,0x0B],[0x98,0x8C,0x00,0x09])
    with assume: assert a == True
    Driver['CAN'].set_signal_IGN(2)
    Driver['CAN'].set_signal_Speed(0)